<template>
  <view>
    <view class="search-box">    
    <!-- 使用自定义my-search组件 -->
    <my-search @zdysj="goToSearch"></my-search>
    </view>
    <!-- 轮播图区 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,i) in swpList" :key="i">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id ">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类区 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item,index) in navList" :key="index" @click="goToCate(item)">
        <image :src="item.image_src" mode=""></image>
      </view>
    </view>
    <!-- 楼层区 -->
    <view class="floor-list">
      <!-- 标题区 -->
      <view class="floor-item" v-for="(item,i) in floorList" :key="i">
        <image class="title" :src="item.floor_title.image_src"></image>
        <!-- 图片区 -->
        <view class="img-box">
          <!-- 左盒子 -->
          <navigator class="img-box-left" :url="item.product_list[0].url" >
            <image :src="item.product_list[0].image_src" mode="widthFix"
              :style="{width:item.product_list[0].image_width+'rpx'}"></image>
          </navigator>
          <!-- 右盒子 -->
          <view class="img-box-right">
            <navigator :url="i2.url" class="right-item" v-for="(i2,i) in item.product_list" :key="i" v-if="i !== 0">
              <image :style="{width: i2.image_width + 'rpx'}" :src="i2.image_src" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
      </view>

    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    mixins: [badgeMix],
    data() {
      return {
        swpList: [],
        navList: [],
        floorList: []
      };
    },
    onLoad() {
      this.getSwiperList();
      this.getNavList();
      this.getFloorList();
    },
    methods: {
      goToSearch(){
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      },
      async getFloorList() {
        const {data: res} = await uni.$http.get('/api/public/v1/home/floordata');
        console.log(res);
        if (res.meta.status !== 200) {
          uni.$showMsg('楼层数据获取失败')
        };
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message;
      },
      goToCate(item) {
        if (item.name === "分类") {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get("/api/public/v1/home/catitems")
        // console.log(res);
        this.navList = res.message
      },
      // 3. 获取轮播图数据的方法
      async getSwiperList() {
        // 3.1 发起请求
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        // console.log(res)
        // 3.2 请求失败
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        // 3.3 请求成功，为 data 中的数据赋值
        this.swpList = res.message
        uni.$showMsg("数据请求成功")
      }
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 99;
  }
  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 15px 0;

    image {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floor-list {
    .title {
      display: flex;
      width: 100%;
      height: 60rpx;
    }

    .img-box {
      display: flex;

      .img-box-left {
        margin-left: 10rpx;
      }

      .img-box-right {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
      }
    }
  }
</style>
